package com.letoken.platform.app.service.impl;

import com.letoken.platform.repo.app.manager.*;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author letokenlabs
 * @date 2022/11/16$
 */
@Service
@Log4j2
public class LetokenTransactionServiceImpl {

    @Autowired
    private TokenManager tokenManager;


//    @Autowired
//    private AppUserAddressManager appUserAddressManager;


    @Autowired
    private AppUserRechargeManager AppUserRechargeManager;

    @Autowired
    private AppUserManager appUserManager;


//    /**
//     * usdt充值回调
//     *
//     * @param req
//     * @return
//     */
//    @Transactional
//    @Override
//    public TransactionCallbackRes transactionCallback(TransactionCallbackReq req) {
////        AppUser appUser = AppLoginTokenRedis.me().getClassObject(WebUtils.getToken());
////        if(null == appUser) {
////            throw new ApiServiceException(ApiResponseEnum.ERROR_004);
////        }
////        String walletAddress = String.valueOf(AppWalletAddresRedis.me().getValue(String.valueOf(appUser.getId())));
////        if (null == walletAddress) {
////            walletAddress = appUserAddressManager.queryWalletAddressById(appUser.getAddressId());
////        }
////        MoralisTransaction check = MoralisUtil.getTransactionByHash(req.getTxHash(), req.getChain());
////        if (ObjectUtil.isEmpty(check)) {
////            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_APP_ORDER_003);
////        }
////        //校验交易是否成功
////        if (!check.getReceiptStatus().equals("1")) {
////            //失败
////            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_APP_ORDER_005);
////        }
////        //成功确认交易合法
////        List<ResultTransaction> result = MoralisUtil.getTokenTransactionByHash(walletAddress, req.getChain(), DateUtil.getTimestamp(), DateUtil.getISO8601Timestamp(new Date()));
////        if (null == result) {
////            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_APP_ORDER_006);
////        }
////        ResultTransaction order = null;
////        for (ResultTransaction transaction : result) {
////            if (transaction.getTransactionHash().equals(req.getTxHash())) {
////                order = transaction;
////            }
////        }
////        if (ObjectUtil.isEmpty(order)) {
////            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_APP_ORDER_006);
////        }
////        AppUserRecharge orderLog = AppUserRechargeManager.querytxHashByhash(req.getTxHash(), req.getChain());
////        if (!ObjectUtil.isEmpty(orderLog)) {
////            //交易hash已存在，重复提交
////            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_APP_ORDER_004);
////        }
////        AppUserRecharge addOrderLog = null;
////        if (appUser.getWalletAddress().equals(order.getFromAddress()) && walletAddress.equals(order.getToAddress())) {
////            //校验通过
////            addOrderLog = new AppUserRecharge();
////            LetokenTokenInfo tokenInfo = AppWalletTokenRedis.me().getClassObject(order.getAddress());
////            if (ObjectUtil.isEmpty(tokenInfo)) {
////                tokenInfo = letokenTokenInfoManager.queryId(order.getAddress());
////                AppWalletTokenRedis.me().put(order.getAddress(), tokenInfo);
////            }
////            addOrderLog.setTxHash(req.getTxHash());
////            addOrderLog.setUserId(appUser.getId());
////            addOrderLog.setAmount(order.getValue());
////            addOrderLog.setTokenId(tokenInfo.getId());
////            addOrderLog.setNetTyp(req.getChain());
////            addOrderLog.setOrderTime(order.getBlockTimestamp());
////            AppUserRechargeManager.add(addOrderLog);
////            //修改用户usdt余额
////            AppUser updateAppUser = appUserManager.queryById(appUser.getId());
////            BigDecimal addValue = new BigDecimal(order.getValue());
//////            BigDecimal add = addValue.add(new BigDecimal(updateAppUser.getUsdtBalance()));
//////            updateAppUser.setUsdtBalance(add.toPlainString());
////            Integer flag = appUserManager.updateById(updateAppUser);
////            if (flag > 0) {
////                //更新成功，添加资金归集任务
////                WalletCollection addTask = new WalletCollection();
////                addTask.setAmount(order.getValue());
////                addTask.setAddressId(appUser.getAddressId());
////                addTask.setOrderId(addOrderLog.getId());
////                addTask.setStatus(0);
////                addTask.setCreateTime(LocalDateTime.now());
////                addTask.setUpdateTime(LocalDateTime.now());
////                appTransferTaskManager.add(addTask);
////            }
////        }
//        return new TransactionCallbackRes();
//    }
}
